package com.tomtaw.web.callnumber;


import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {


    public static boolean isBlank(CharSequence text) {
        if (text == null || text.length() == 0) return true;
        for (int i = 0, L = text.length(); i < L; i++) {
            if (!Character.isWhitespace(text.charAt(i))) return false;
        }
        return true;
    }

    /**
     * 密码是8-16位，有数字和字母密码
     */
    public static boolean is8To16Password(String password) {
        if (TextUtils.isEmpty(password)) {
            return false;
        }

        String regex = "^((?!^\\d+$)(?!^[a-zA-Z]+$)[0-9A-Za-z]{8,16})$";
        return password.matches(regex);
    }


    /**
     * 是4-10位，有数字和字母密码
     */
    public static boolean is4To10Password(String password) {
        if (TextUtils.isEmpty(password)) {
            return false;
        }

        String regex = "^((?!^\\d+$)(?!^[a-zA-Z]+$)[0-9A-Za-z]{4,10})$";
        return password.matches(regex);
    }


    /**
     * 是否手机号
     *
     * @param phoneNum
     * @return
     */
    public static boolean isPhoneNum(CharSequence phoneNum) {
        if (phoneNum == null || phoneNum.length() != 11) return false;
        if (!TextUtils.isDigitsOnly(phoneNum)) return false;
        Pattern p = Pattern.compile("^((13[0-9])|(14[0-9])|(15[^4,\\D])|(16[0-9])|(17[0-9])|(18[0-9])|(19[0-9]))\\d{8}$");
        return p.matcher(phoneNum).find();
    }

    public static String hideStrind(String src) {
        StringBuilder stringBuilder = new StringBuilder(src);
        int len = stringBuilder.length();
        int start = len/4;
        int end = len*3/4;
        stringBuilder.replace(start, end, "****");
        return stringBuilder.toString();
    }

    /**
     * 密码是否过于简单,8-20位，要有数字和密码
     */
    public static boolean isSimplePassword( String password) {
        if (TextUtils.isEmpty(password)) {
            return true;
        }

        String regex = "^((?!^\\d+$)(?!^[a-zA-Z]+$)(?!^[~!&_#@]+$).{8,20})$";
        return !password.matches(regex);
    }

  /**
   * 去除字符串中的空格、回车、换行符、制表符
   */
  public static String replaceBlank(String str) {
    String dest = "";
    if (str!=null) {
      Pattern p = Pattern.compile("\\s*|\t|\r|\n");
      Matcher m = p.matcher(str);
      dest = m.replaceAll("");
    }
    return dest;
  }

}
